﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using RN.ERP.BLL;
using Newtonsoft.Json;
using RN.ERP.DTO;
using System.ServiceModel.Activation;

namespace RN.ERP
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“OrderService”。
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class OrderService : IOrderService
    {
        BaseOrderBLL basicBll = (BaseOrderBLL)BllFactory.CreateBll(BllType.Order, Utilities.GetDbType());
        public string GetOrders(string queryCondition, Guid userGuid, int pageCount, int pageSize)
        {
            return JsonConvert.SerializeObject(basicBll.GetOrders(JsonConvert.DeserializeObject<IList<DTO.QueryInvoiceInfoCondition>>(queryCondition), userGuid, pageCount, pageSize));
        }
        public int GetOrderCount(string queryCondition, Guid userGuid)
        {
            return basicBll.GetOrderCount(JsonConvert.DeserializeObject<IList<DTO.QueryInvoiceInfoCondition>>(queryCondition), userGuid);
        }
        public string GetOrderInfo(string orderId)
        {
            return JsonConvert.SerializeObject(basicBll.GetOrderInfo(orderId));
        }
        public string GetProducts(string query, string areacode)
        {
            List<ProductCondition> condition = JsonConvert.DeserializeObject<List<ProductCondition>>(query);
            return JsonConvert.SerializeObject(basicBll.GetProducts(condition, areacode));
        }
        public string AddOrder(string payInfo)
        {
            CaPayInfo caPayInfo = JsonConvert.DeserializeObject<CaPayInfo>(payInfo);
            return JsonConvert.SerializeObject(basicBll.AddOrder(caPayInfo));
        }
        public string EditOrder(string payInfo)
        {
            CaPayInfo caPayInfo = JsonConvert.DeserializeObject<CaPayInfo>(payInfo);
            return JsonConvert.SerializeObject(basicBll.EditOrder(caPayInfo));
        }
        public string DeleteOrder(Guid orderGuid)
        {
            return JsonConvert.SerializeObject(basicBll.DeleteOrder(orderGuid));
        }
        public string SubmitOrder(string payInfo)
        {
            CaPayInfo caPayInfo = JsonConvert.DeserializeObject<CaPayInfo>(payInfo);
            return JsonConvert.SerializeObject(basicBll.SubmitOrder(caPayInfo));
        }
        public string GetInvoicesList(string queryCondition, int pageCount, int pageSize)
        {
            return JsonConvert.SerializeObject(basicBll.GetInvoicesList(JsonConvert.DeserializeObject<IList<DTO.QueryInvoiceInfoCondition>>(queryCondition), pageCount, pageSize));
        }
        public string AddInvoices(string relation)
        {
            InvoiceOrderRelation invoices = JsonConvert.DeserializeObject<InvoiceOrderRelation>(relation);
            return JsonConvert.SerializeObject(basicBll.AddInvoices(invoices));
        }
        public int GetInvoicesCount(string queryCondition)
        {
            return basicBll.GetInvoicesCount(JsonConvert.DeserializeObject<IList<DTO.QueryInvoiceInfoCondition>>(queryCondition));
        }

        public byte[] GetCorpCAPdf(Guid corpUserGuid)
        {
            return basicBll.GetCorpCAPdf(corpUserGuid);
        }
        public byte[] GetPsnCAPdf(Guid empGuid)
        {
            return basicBll.GetPsnCAPdf(empGuid);
        }
        public byte[] GetPsnKeyPdf(Guid empGuid)
        {
            return basicBll.GetPsnKeyPdf(empGuid);
        }
        public string ValidateKeyStatus(int caSubId, int dogId, string name)
        {
            return JsonConvert.SerializeObject(basicBll.ValidateKeyStatus(caSubId, dogId, name));
        }
        public string UpdateCert(
            int caSubId,
            string Entity,
            Guid EntityGuid,
            string EntityName,
            string KeySN,
            string operation)
        {
            return JsonConvert.SerializeObject(basicBll.UpdateCert(caSubId, Entity, EntityGuid, EntityName, KeySN, operation));
        }
        public string VerifyOrder(List<Guid> orderGuid, Guid userGuid)
        {
            return JsonConvert.SerializeObject(basicBll.VerifyOrder(orderGuid, userGuid));
        }
        public string GetProductGroup()
        {
            return JsonConvert.SerializeObject(basicBll.GetProductGroup());
        }

        public string GetProductsByUserGuid(string userGuid, string areacode, int type)
        {
            return JsonConvert.SerializeObject(basicBll.GetProductsByUserGuid(userGuid, areacode, type));
        }
        public string OrderAudit(List<Guid> orderGuid, Guid userGuid)
        {
            return JsonConvert.SerializeObject(basicBll.OrderAudit(orderGuid, userGuid));
        }

        public string GetZBPayList(string queryCondition, int pageCount, int pageSize)
        {
            return JsonConvert.SerializeObject(basicBll.GetZBPayList(JsonConvert.DeserializeObject<IList<DTO.QueryZBTenderInfoCondition>>(queryCondition), pageCount, pageSize));
        }
        public string GetZBPayCount(string queryCondition)
        {
            return basicBll.GetZBPayCount(JsonConvert.DeserializeObject<IList<DTO.QueryZBTenderInfoCondition>>(queryCondition));
        }
        public string PaySaveZBRemark(int ID, bool Processed, string Remark, int type)
        {
            return JsonConvert.SerializeObject(basicBll.PaySaveZBRemark(ID, Processed, Remark, type));
        }
        public string GetZBRemark(int ID, int type)
        {
            return JsonConvert.SerializeObject(basicBll.GetZBRemark(ID, type));
        }

        public string GetCAInfo(string corpUserGuid)
        {
            return JsonConvert.SerializeObject(basicBll.GetCAInfo(corpUserGuid));
        }

        #region 通用
        public string GetCommonOrders(string queryCondition, Guid userGuid, int pageCount, int pageSize)
        {
            return JsonConvert.SerializeObject(basicBll.GetCommonOrders(
                JsonConvert.DeserializeObject<IList<DTO.QueryCommonInvoiceCondition>>(queryCondition)
                , userGuid, pageCount, pageSize));
        }

        public int GetCommonOrderCount(string queryCondition, Guid userGuid)
        {
            return basicBll.GetCommonOrderCount(JsonConvert.DeserializeObject<IList<DTO.QueryCommonInvoiceCondition>>(queryCondition), userGuid);
        }

        public string GetCommonOrderInfo(string orderId)
        {
            return JsonConvert.SerializeObject(basicBll.GetCommonOrderInfo(orderId));
        }

        public string AddCommonOrder(string payInfo)
        {
            PayInfo pInfo = JsonConvert.DeserializeObject<PayInfo>(payInfo);
            return JsonConvert.SerializeObject(basicBll.AddCommonOrder(pInfo));
        }

        public string EditCommonOrder(string payInfo)
        {
            PayInfo pInfo = JsonConvert.DeserializeObject<PayInfo>(payInfo);
            return JsonConvert.SerializeObject(basicBll.EditCommonOrder(pInfo));
        }

        public string DeleteCommonOrder(Guid orderGuid)
        {
            return JsonConvert.SerializeObject(basicBll.DeleteCommonOrder(orderGuid));
        }

        public string SubmitCommonOrder(string payInfo)
        {
            PayInfo pInfo = JsonConvert.DeserializeObject<PayInfo>(payInfo);
            return JsonConvert.SerializeObject(basicBll.SubmitCommonOrder(pInfo));
        }

        /// <summary>
        /// 获取做key信息列表
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public string GetMakeKeyInfos(string condition, int pageIndex, int pageSize, out int count)
        {
            MakeKeyInfosCondition model = JsonConvert.DeserializeObject<MakeKeyInfosCondition>(condition);
            return JsonConvert.SerializeObject(basicBll.GetMakeKeyInfos(model, pageIndex, pageSize, out count));
        }
        #endregion
    }
}
